//
//  UserAccountViewModel.swift
//  SinaWeibo
//
//  Created by 石林 on 15/11/17.
//  Copyright © 2015年 石林. All rights reserved.
//

import UIKit
import AFNetworking

class UserAccountViewModel: NSObject {
    
    var account: UserAccount?
    
    var userLogin: Bool {
        
        return account?.access_token != nil
    }
    
    override init() {
        account = UserAccount.loadAccount()
    }
    
    var userName: String? {
        
        return account?.name
        
    }
    
    var iconURL: NSURL? {
        
        return NSURL(string: account?.avatar_large ?? "")
        
    }

    func loadAccessToken(code: String, finished: (error: NSError?) -> ()) {
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let parameters = ["client_id": client_id,"client_secret": client_secret,"grant_type": "authorization_code","code": code,"redirect_uri": redirect_uri]
        //        let parameter = ["client_id": client_id,"client_secret":client_secret,"grant_type" : "authorization_code","code":code,"redirect_uri":redirect_uri]
        
        let AFN = AFHTTPSessionManager()
        AFN.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        //        AFN.responseSerializer = AFHTTPResponseSerializer()
        AFN.POST(urlString, parameters: parameters, success: { (_, result) -> Void in
            //
            //            let str = NSString(data: result as! NSData, encoding: NSUTF8StringEncoding)
            //            print(str)
            print(result)
            if let dict = result as? [String: AnyObject] {
                
                let account = UserAccount(dict: dict)
                print(account)
                self.loadUserInfo(account,finished: finished)
                //                let access_token = dict["access_token"] as? String
                //                let uid = dict["uid"] as? String
                //                //后端给的数据一定能拿到数据
                //                self.loadUserInfo(access_token!, uid: uid!)
            }
            
            }) { (_, error) -> Void in
                print(error)
        }
        
    }
    
    
    private func loadUserInfo(account: UserAccount,finished:(error:NSError?) -> ()) {
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        let parameters = ["access_token": account.access_token!,"uid": account.uid!]
        
        let AFN = AFHTTPSessionManager()
        
        AFN.GET(urlString, parameters: parameters, success: { (_, result) -> Void in
            print(result)
            
            if let dict = result as? [String: AnyObject] {
                
                account.avatar_large = dict["avatar_large"] as? String
                account.name = dict["name"] as? String
                account.saveAccount()
                print("account = ",account)
                finished(error: nil)
            }
            }) { (_, error) -> Void in
                print(error)
                finished(error: error)
        }
        
    }
}
